#include<stdio.h>
#include<stdlib.h>
#include<string.h>

//该函数分割字符串转化为整数
int* input(char *fmttime, int num[3]){

	char* token = strtok(fmttime,":");
	int i = 0;

	while(token != NULL)
	{
		num[i] = atoi(token);
		token = strtok(NULL,":");
		i = i + 1;
	}
	return num;
}

//该函数计算时长
int seconds(int array[3]){
	int h = array[0]*3600;
	int m = array[1]*60;
	int s = array[2];
	int num = h + m + s;
	return num;
}

//该函数格式化时长
char* fmt_seconds(int seconds, char fmt_rtn[8]){

	char hh[3] = {};
	char mm[3] = {};
	char ss[2] = {};

	int h = seconds / 3600;
	sprintf(hh,"%02d:",h);
	strcat(fmt_rtn,hh);

	int m = (seconds - 3600*h) / 60;
	sprintf(mm,"%02d:",m);
	strcat(fmt_rtn,mm);

	int s = (seconds - 3600*h) % 60;
	sprintf(ss,"%02d",s);
	strcat(fmt_rtn,ss);

	return fmt_rtn;
}

int main(int argc, char* argv[]){

	if( argc != 3)
	{
		printf("%s [src.mp4] [des.mp4]\n",argv[0]);
		exit(0);
	}
	char start[8];
	char start_s[8];
	int start_num[3];

	char end[8];
	int end_num[3];

	//时长
	int t;
	//时长格式化后的字符串
	char rtn[8] = "";
	//拼接成系统命令
	char cmd[1000];

	printf("输入起始时刻:");
	scanf("%s",start);
	strcpy(start_s,start);
	
	input(start,start_num);

	printf("输入结束时刻:");
	scanf("%s",end);
	input(end,end_num);

	t =  seconds(end_num) - seconds(start_num);

 	fmt_seconds(t,rtn);
	printf("格式化后的时长:%s\n",rtn);

	sprintf(cmd,\
			"nohup ffmpeg -ss %s -t %s -i ./%s -vcodec copy -acodec copy ./%s >/dev/null 2>&1;wait",start_s,rtn,argv[1],argv[2]);
	printf("执行的命令:%s\n",cmd);
	system(cmd);
}
